package com.revenant.driver.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.revenant.driver.R;
import com.revenant.driver.adapter.FaPiaoTaiTouItemAdapter;
import com.revenant.driver.base.BaseActivity;
import com.revenant.driver.bean.ShipperBean.FaPiaoTaiTouListBean;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;

import static com.revenant.driver.utils.Constants.Fa_Piao_TaiTou_Delete;
import static com.revenant.driver.utils.Constants.Fa_Piao_TaiTou_List;

public class ReceiptTitleActivity extends BaseActivity {
    @BindView(R.id.publish_goods_recyclerview)
    RecyclerView       publishGoodsRecyclerview;
    @BindView(R.id.smartrefresh)
    SmartRefreshLayout smartrefresh;

    private FaPiaoTaiTouItemAdapter mAdapter;
    private int                     pagernum = 1;

    @Override
    public int getContentViewResId() {
        return R.layout.activity_receipt_title;
    }

    @Override
    public void initView() {
        setcenterTitle("推送货源");
        mAdapter = new FaPiaoTaiTouItemAdapter();
        publishGoodsRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        publishGoodsRecyclerview.setAdapter(mAdapter);
        smartrefresh.autoRefresh();
    }

    @Override
    public void initData() {
        smartrefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pagernum++;
                initDataRecyclerview();
                smartrefresh.finishLoadMore(true);//加载完成
            }
        });
        //刷新
        smartrefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pagernum = 1;
                mAdapter.setNewData(null);
                initDataRecyclerview();
                smartrefresh.finishRefresh(true);//刷新完成
            }
        });

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.edit:
                        Bundle bundle = new Bundle();
                        bundle.putInt("fapiaotaitoutype", 1);
                        bundle.putInt("fapiaotaitouid", mAdapter.getData().get(position).getId());
                        Intent intent = new Intent();
                        intent.setClass(ReceiptTitleActivity.this, EditReceiptTitleActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        break;
                    case R.id.delete:
                        deleteid(mAdapter.getData().get(position).getId(), position);
                        break;
                }
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("fapiaotaitoutype", 0);
                bundle.putInt("fapiaotaitouid", mAdapter.getData().get(position).getId());
                Intent intent = new Intent();
                intent.setClass(ReceiptTitleActivity.this, EditReceiptTitleActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });
    }

    private void initDataRecyclerview() {
        OkGo.<String>get(Fa_Piao_TaiTou_List)

                .params("accountId", SPUtils.getAccounId(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        FaPiaoTaiTouListBean shipperPublishedBean = GsonUtil.parseJsonWithGson(response.body(), FaPiaoTaiTouListBean.class);
                        int pageNum = shipperPublishedBean.getData().getPageNum();
                        int pages = shipperPublishedBean.getData().getPages();
                        if (shipperPublishedBean.getCode().equals("0")) {
                            if (pageNum == 1) {
                                mAdapter.setNewData(shipperPublishedBean.getData().getResult());
                            } else if (pageNum <= pages) {
                                mAdapter.addData(shipperPublishedBean.getData().getResult());
                            } else {
                                mAdapter.loadMoreEnd();
                            }
//

                        } else {
                            mAdapter.loadMoreFail();
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }

    @Override
    public void leftbarclick() {
        super.leftbarclick();
        finish();
    }


    private void deleteid(int id, int posi) {
        OkGo.<String>post(Fa_Piao_TaiTou_Delete)

                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        FaPiaoTaiTouListBean shipperPublishedBean = GsonUtil.parseJsonWithGson(response.body(), FaPiaoTaiTouListBean.class);

                        if (shipperPublishedBean.getCode().equals("0")) {
                            showToast(shipperPublishedBean.getMsg());
                            mAdapter.remove(posi);
//

                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }

}
